package org.azmat.demojpa.Controller.JPA;

import org.azmat.demojpa.SQL.JPA.Entity.Course;
import org.azmat.demojpa.SQL.JPA.Service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/updateCourse/{id}")
    public ModelAndView toupdateCourse(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("course", courseService.findById(id));
        mv.setViewName("updateCourse");
        return mv;
    }

    // 获取新数据，修改数据库
    @PostMapping("/updateCourse")
    public ModelAndView updateCourse(Course course) {
        ModelAndView mv = new ModelAndView();
        courseService.updateCourses(course);
        mv.setViewName("redirect:/course/findAll");
        return mv;
    }

    // 将课程添加到数据库
    @PostMapping("/addCourse")
    public ModelAndView addCourse(Course course) {
        ModelAndView mv = new ModelAndView();
        courseService.addCourses(course);
        mv.setViewName("redirect:/course/findAll");
        return mv;
    }

    // 用于超链接，呈现添加课程的视图
    @GetMapping("/addCourse")
    public ModelAndView MVAddCourse() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("addCourse");
        return mv;
    }

    // 删除课程
    @PostMapping("/deleteCourse/{id}")
    public ModelAndView deleteCourse(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView();
        courseService.deleteCourses(id);
        mv.setViewName("redirect:/course/findAll");
        return mv;
    }

    // 查询所有课程，无排序
    @GetMapping("/findAll")
    public ModelAndView findAll1() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findAll());
        System.out.println("findAll1" + courseService.findAll().size());
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询所有课程，有排序
    @GetMapping("/findAll2")
    public ModelAndView findAll2() {
        ModelAndView mv = new ModelAndView();
        List<Course> courses = courseService.findAll(Sort.by(Sort.Direction.DESC, "courseScore"));
        mv.addObject("courses", courses);
        mv.setViewName("CoursesfindAll2");
        return mv;
    }

    // 查询所有课程，有排序，有分页
    @GetMapping("/findAll3")
    public ModelAndView findAll3(@RequestParam(value = "start", defaultValue = "0") int start,
                                 @RequestParam(value = "size", defaultValue = "10") int size) {
        ModelAndView mv = new ModelAndView();
        Pageable pageable = PageRequest.of(start, size, Sort.by(Sort.Direction.DESC, "courseId"));
        // 也可以不排序
        Page<Course> page = courseService.findAll(pageable);
        mv.addObject("page", page);
        mv.setViewName("coursePage");
        return mv;
    }

    // 查询某个编号的课程
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("course", courseService.findById(id));
        mv.setViewName("CoursesfindById");
        return mv;
    }


    // 查询学分为xx的选修(或必修)课
    @GetMapping("/findByCourseScore/{courseScore}/{courseType}")
    public ModelAndView findByCourseScore(@PathVariable("courseScore") int courseScore,
                                          @PathVariable("courseType") String courseType) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findByCourseScoreAndCourseType(courseScore, courseType));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询学分为xx或者选修(或必修)课
    @GetMapping("/findByCourseScoreOrCourseType/{courseScore}/{courseType}")
    public ModelAndView findByCourseScoreOrCourseType(@PathVariable("courseScore") int courseScore,
                                                      @PathVariable("courseType") String courseType) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findByCourseScoreOrCourseType(courseScore, courseType));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询课时在xxx到xxx的课程
    @GetMapping("/findByCourseTimeBetween/{courseTime1}/{courseTime2}")
    public ModelAndView findByCourseTimeBetween(@PathVariable("courseTime1") int courseTime1,
                                                @PathVariable("courseTime2") int courseTime2) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findByCourseHourBetween(courseTime1, courseTime2));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询课时小于xx的课程
    @GetMapping("/findByCourseHourLessThan/{courseHour}")
    public ModelAndView findByCourseHourLessThan(@PathVariable("courseHour") int courseHour) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findByCourseHourLessThan(courseHour));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询名称包含xxx的课程
    @GetMapping("/findByCourseNameLike/{courseName}")
    public ModelAndView findByCourseNameLike(@PathVariable("courseName") String courseName) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.findByCourseNameLike(courseName));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 自定义查询，Dao层需动手编写SQL或JPQL语句
    // 查询学分大于某个值的课程信息，DAO层使用JPQL
    @GetMapping("/selectCourse1/{courseScore}")
    public ModelAndView selectCourse1(@PathVariable("courseScore") int courseScore) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.selectCourses1(courseScore));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }


    // 查询学分大于某个值，并且课时大于某个值的课程，DAO层使用JPQL，多个参数。
    @GetMapping("/selectCourse2/{courseScore}/{courseTime}")
    public ModelAndView selectCourse2(@PathVariable("courseScore") int courseScore,
                                      @PathVariable("courseTime") int courseTime) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.selectCourses2(courseScore, courseTime));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询学分大于某个值的课程信息，DAO层使用SQL语句
    @GetMapping("/selectCourse3/{courseScore}")
    public ModelAndView selectCourse3(@PathVariable("courseScore") int courseScore) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.selectCourses3(courseScore));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    // 查询学分大于某个值的课程信息，使用命名参数
    @GetMapping("/selectCourse4/{courseScore}")
    public ModelAndView selectCourse4(@PathVariable("courseScore") int courseScore) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.selectCourses4(courseScore));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }


    // 模糊查询
    @GetMapping("/selectCourse5/{courseName}")
    public ModelAndView selectCourse5(@PathVariable("courseName") String courseName) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("courses", courseService.selectCourses5(courseName));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }

    @GetMapping("findCourseStudents")
    public ModelAndView findCourseStudents(int courseId) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("students", courseService.findById(courseId));
        mv.setViewName("CoursesfindAll1");
        return mv;
    }



}
